<?php
declare(strict_types=1);

namespace App\Process;

use App\Game\Core\Robot;
use App\Game\Timer\GameTimer;
use Hyperf\Process\AbstractProcess;
use Hyperf\Process\Annotation\Process;

/**
 * @Process(name="timer_process")
 */
class TimerProcess extends AbstractProcess
{

    /**
     * 进程数量
     * @var int
     */
    public $nums = 1;

    /**
     * 进程名称
     * @var string
     */
    public $name = 'timer-process';

    public function handle(): void
    {
        $game_conf = config('game');
        $addkey = $game_conf['user_room_timer_add'];
        $delkey = $game_conf['user_room_timer_del'];
        $redis = redis();
        while (true) {
            //优先销毁
            $deltask = $redis->rpop($delkey);
            if ($deltask) {
                $timerInfo = json_decode($deltask, true);
                if ($timerInfo) {
                    $timerId = $timerInfo['id'];
                    $exec = $timerInfo['exec'];
                    $res = \Swoole\Timer::clear((int)$timerId);
                    ddTitle('cleanTimer slot1 : ' . $exec . ' res : ' . $res . ' | id : ' .
                        $timerId);
                }
            }
            //再执行注册
            $addtask = $redis->rpop($addkey);    //rPop
            if ($addtask) {
                $timerArr = json_decode($addtask, true);
                $wait_time = $timerArr['wait_time'] * 1000;
                $timer = $timerArr['timer'];
                $room_no = $timerArr['room_no'];
                switch ($timer) {
                    case GameTimer::$ASSIGN_ROBOT_TIMER :
                        $name = 'assign_robot';
                        break;
                    case GameTimer::$CHANGE_CARD_TIMER :
                        $name = 'change_card';
                        break;
                    case GameTimer::$CHOOSE_COLOR_TIMER:
                        $name = 'choose_color';
                        break;
                    case GameTimer::$OUT_CARD_TIMER:
                        $name = 'out_card';
                        break;
                    case GameTimer::$CHECK_HPK_TIMER:
                        $name = 'check_hpg';
                        break;
                    case GameTimer::$ADD_DJS_ROBOT_TIMER:
                        $name = 'add_djs_robot';
                        break;
                }
                $time = time();
                $timerId = \Swoole\Timer::after((int)$wait_time, function () use (
                    $timer, $room_no,
                    $name, $time
                ) {
                    switch ($timer) {
                        case GameTimer::$ASSIGN_ROBOT_TIMER :
                            ddTitle('assign_robot_timer slot1 :' . $room_no);
                            GameTimer::do_assign_robots($room_no);
                            break;
                        case GameTimer::$CHANGE_CARD_TIMER :
                            ddTitle('change_card_timer slot1 :' . $room_no);
                            GameTimer::do_change_card($room_no);
                            break;
                        case GameTimer::$CHOOSE_COLOR_TIMER:
                            ddTitle('choose_color_timer slot1 :' . $room_no);
                            GameTimer::do_choose_color($room_no);
                            break;
                        case GameTimer::$OUT_CARD_TIMER:
                            ddTitle('out_card_timer slot1 :' . $room_no);
                            GameTimer::do_out_card($room_no, [], true);
                            break;
                        case GameTimer::$CHECK_HPK_TIMER:
                            ddTitle('check_hpk_timer slot1 :' . $room_no);
                            GameTimer::do_check_hpk($room_no);
                            break;
                        case GameTimer::$ADD_DJS_ROBOT_TIMER:
                            ddTitle('add_djs_robot_timer slot1 :' . $room_no);
                            Robot::addDjsRobots($room_no);
                            break;
                    }
                });
                addTimer($room_no, $timerId, $name, $time);
            }
        }
    }
}
